﻿//-------------------------------------------------------------------------------------------------
// <copyright file="TextHandlers.cs" company="Anthony Adam">
// Copyright (c) Anthony Adam 2012. All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------
// ReSharper disable once CheckNamespace
namespace SuperStarRounds
{
  using System;
  using System.Collections.Generic;
  using System.Text;
  using System.Text.RegularExpressions;
  using System.Windows.Forms;

  /// <summary>
  /// This class contains the basic text utility functions.
  /// examples include the word count of some given text, 
  /// camel capitalisation of text, and so on.
  /// </summary>
  public static class TextHandlers
  {
    /// <summary>
    /// The list of words not capitalised unless they are the first word.
    /// </summary>
    private static readonly List<string> NonCapitalisedWords = new List<string>() 
            {
              "for", "and", "nor", "but", "or", "yet", "so",              // Non Capitalised Coordinating Conjunctions
              "the", "a", "an", "as", "is",                               // Non Capitalised Determiners
              "at", "by", "in", "into", "of", "on", "onto", "to", "via"   // Non Capitalised Prepositions
            };

    /// <summary>
    /// Function to return a basic word count of words identified in the provided text
    /// </summary>
    /// <param name="value">The text to be word counted</param>
    /// <returns>The count of words in the text provided</returns>
    public static int WordCount(string value)
    {
      MatchCollection words = Regex.Matches(value, @"[\S]+");
      return words.Count;
    }

    /// <summary>
    /// Camel Capitalises the text part of the provided control.
    /// </summary>
    /// <param name="sender">Holds the object instantiating this event</param>
    /// <param name="e">Holds the event arguments</param>
    public static void CamelCapitalize(object sender, EventArgs e)
    {
      var senderControl = (Control) sender;
      var myNewCamelCase = new StringBuilder();
      var words = Regex.Matches(senderControl.Text, @"[\S]+");

      if (words.Count < 1) return;

      for (var i = 0; i < words.Count; i++)
      {
        var word = words[i].ToString().ToLower();
        var wordTemp = word.ToCharArray();
        if (i == 0 || (!IsANonCapitalisingConjunctive(word)))
          wordTemp[0] = char.ToUpper(wordTemp[0]);
        
        if (i > 0) myNewCamelCase.Append(" ");
        
        myNewCamelCase.Append(new string(wordTemp));
      }

      senderControl.Text = myNewCamelCase.ToString();
    }

    /// <summary>
    /// Makes the text of the passed control all lowercase.
    /// </summary>
    /// <param name="sender">Holds the object instantiating this event</param>
    /// <param name="e">Holds the event arguments</param>
    public static void MakeLower(object sender, EventArgs e)
    {
      var senderControl = (Control)sender;
      var words = Regex.Matches(senderControl.Text.ToLower(), @"[\S]+");
      var newText = new StringBuilder();
      for (var i = 0; i < words.Count; i++)
      {
        var tempWord = words[i].ToString();
        tempWord = tempWord.Equals("i") ? "I" : tempWord;
        tempWord = tempWord.Equals("i)") ? "I)" : tempWord;
        tempWord = tempWord.Equals("ii") ? "II" : tempWord;
        tempWord = tempWord.Equals("ii)") ? "II)" : tempWord;
        tempWord = tempWord.Equals("iii") ? "III" : tempWord;
        tempWord = tempWord.Equals("iii)") ? "III)" : tempWord;
        tempWord = tempWord.Equals("iv") ? "IV" : tempWord;
        tempWord = tempWord.Equals("iv)") ? "IV)" : tempWord;
        tempWord = tempWord.Equals("v") ? "V" : tempWord;
        tempWord = tempWord.Equals("v)") ? "V)" : tempWord;
        tempWord = tempWord.Equals("vi") ? "VI" : tempWord;
        tempWord = tempWord.Equals("vi)") ? "VI)" : tempWord;
        tempWord = tempWord.Equals("vii") ? "VII" : tempWord;
        tempWord = tempWord.Equals("vii)") ? "VII)" : tempWord;
        tempWord = tempWord.Equals("viii") ? "VIII" : tempWord;
        tempWord = tempWord.Equals("viii)") ? "VIII)" : tempWord;
        tempWord = tempWord.Equals("ix") ? "IX" : tempWord;
        tempWord = tempWord.Equals("ix)") ? "IX)" : tempWord;
        tempWord = tempWord.Equals("x") ? "X" : tempWord;
        tempWord = tempWord.Equals("x)") ? "X)" : tempWord;
        newText.Append(tempWord);
        if (i < (words.Count - 1)) newText.Append(" ");
      }

      senderControl.Text = newText.ToString();
    }

    /// <summary>
    /// Utility tester to see if the word passed is one of the non capitalising words
    /// </summary>
    /// <param name="value">The word to be checked</param>
    /// <returns>Word is non capitalisor (true) or must be capitalised (false)</returns>
    private static bool IsANonCapitalisingConjunctive(string value)
    {
      return NonCapitalisedWords.Exists(value.Equals);
    }
  }
}
